﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace MyClever.Lib
{
    public class PluginPath : MarshalByRefObject
    {
        private string maintainer;
        private string name;
        private string version;
        private string file;
        private FileInfo fileInfo;

        public PluginPath(string pluginPath)
            : this(new FileInfo(pluginPath))
        {

        }

        public PluginPath(FileInfo pluginPath)
            : this(ExtractMaintainer(pluginPath), ExtractName(pluginPath), ExtractVersion(pluginPath), pluginPath.Name)
        {

        }

        public PluginPath(string pluginMaintainer, string pluginName, string pluginVersion, string pluginFile)
        {
            this.maintainer = pluginMaintainer;
            this.name = pluginName;
            this.version = pluginVersion;
            this.file = pluginFile;
        }

        public string Maintainer
        {
            get { return this.maintainer; }
        }

        public string Name
        {
            get { return this.name; }
        }

        public string Version
        {
            get { return this.version; }
        }

        public string File
        {
            get { return this.file; }
        }

        public string FileExtension
        {
            get { return this.FullPathInfo.Extension; }
        }        

        public string BasePath
        {
            get 
            {
                return Path.Combine(PluginManager.Instance.PluginRootDirectory, this.maintainer, this.name, this.version);
            }
        }

        public string RelativeBasePath
        {
            get
            {
                return Path.Combine("Plugins", this.maintainer, this.name, this.version);
            }
        }

        public string DataPath
        {
            get 
            {
                return Path.Combine(this.BasePath, "data");
            }
        }

        public string RelativeDataPath
        {
            get
            {
                return Path.Combine(this.RelativeBasePath, "data");
            }
        }

        public string LibraryPath
        {
            get 
            {
                return Path.Combine(this.BasePath, "lib");
            }
        }

        public string RelativeLibraryPath
        {
            get
            {
                return Path.Combine(this.RelativeBasePath, "lib");
            }
        }

        public string FullPath
        {
            get
            {
                return Path.Combine(this.DataPath, this.file);
            }
        }

        public FileInfo FullPathInfo
        {
            get
            {
                return new FileInfo(this.FullPath);
            }
        }

        private static string ExtractMaintainer(FileInfo pluginPath)
        {
            return pluginPath.Directory.Parent.Parent.Parent.Name;
        }

        private static string ExtractName(FileInfo pluginPath)
        {
            return pluginPath.Directory.Parent.Parent.Name;
        }

        private static string ExtractVersion(FileInfo pluginPath)
        {
            return pluginPath.Directory.Parent.Name;
        }
    }
}
